﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;

using System;
using System.Collections.Generic;
using System.IO;

namespace RabbitDownload.WebSite.Bcy
{
    internal class BcyDownload
    {/// <summary>
     /// 开始下载
     /// </summary>
     /// <param name="Title">标题（作为文件夹名字）</param>
     /// <param name="Content">内容（包括div，用户介绍）</param>
     /// <param name="ImgList">图片</param>
        internal static void Start(String Title, string Content, List<string> ImgList)
        {
            if (ImgList.Count == 0 && Content == "")
                return;
            string FloderPath = StaticValueOld.DownloadPath + TextHelper.ReplaceBadChar(Title) + "\\";

            if (!FloderHelper.FloderExits(FloderPath, true))
            {
                return;
            }

            foreach (string line in ImgList)
            {
                String Imglink = line;

               WebHelper.DownloadToFloder(Imglink, FloderPath, WebHelper.PcWebHeader(Imglink));
            }
            SaveContent(FloderPath, Content);
        }

        public static string GetFileName(String FloderPath, string FileUrl)
        {
            String SavePath = "";

            if (Path.GetExtension(FileUrl) == ".jpg" || Path.GetExtension(FileUrl) == ".png" ||
                Path.GetExtension(FileUrl) == ".jpeg" || Path.GetExtension(FileUrl) == ".gif" ||
                Path.GetExtension(FileUrl) == ".bmp")//前真后不做，能省一次运算
                SavePath = FloderPath + Path.GetFileName(FileUrl);
            else
                SavePath = FloderPath + Path.GetFileName(FileUrl) + DateTime.Now.ToFileTime() + ".jpg";

            return SavePath;
        }

        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="floderPath"></param>
        /// <param name="content"></param>
        private static void SaveContent(string floderPath, string content)
        {
            if (content != "")
                FileHelper.AppendText(floderPath + "content.txt", content);
        }
    }
}